package com.cloudcc.mobile.view.test;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.cloudcc.cloudframe.net.ErrorInfo;
import com.cloudcc.cloudframe.net.async.AsyncClient;
import com.cloudcc.cloudframe.net.async.JsonRequest;
import com.cloudcc.cloudframe.util.GsonUtil;
import com.cloudcc.mobile.R;
import com.cloudcc.mobile.dialog.MakeTureDialog;
import com.cloudcc.mobile.manager.RunTimeManager;
import com.google.gson.JsonObject;
import com.litesuits.android.log.Log;
import com.loopj.android.http.RequestParams;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import butterknife.Bind;

/**
 * 测试第一步骤 输入手机号码
 * 
 * @Description: TODO()
 * @author wangzhiqiang
 * @date 2016-3-2 下午3:34:30
 * 
 */
public class Step2Fragment extends BaseStepFragment {
	private String mEns = RunTimeManager.getInstance().getlanguage();

	@Bind(R.id.xinxitijiao)
	Button xinxitijiao;

	@Bind(R.id.edtmyname)
	EditText edtmyname;


	@Bind(R.id.edtname)
	EditText edtname;

	MakeTureDialog dialog;

	@Override
	public void init() {
		super.init();
		// initPhone();
		// initEditText();
		xinxitijiao.setEnabled(false);
		dialog = new MakeTureDialog(getActivity(), R.style.DialogLoadingTheme);
		addlistener();

	}

	String myname = "";
	String name = "";
	String servicename = "";

	private void addlistener() {
		// 提交监听
		xinxitijiao.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				myname = edtmyname.getText().toString().trim();
				name = edtname.getText().toString().trim();
				if ("".equals(myname)) {
					
					if ("en".equals(mEns)) {
						dialog.show();
						dialog.setTitle("Please enter your name!");
						}else{
						dialog.show();
						dialog.setTitle("请输入您的姓名");
					}
//					Toast.makeText(mContext, "请输入您的姓名", 1).show();
					return;
				}
				if ("".equals(name)) {
					if ("en".equals(mEns)) {
						dialog.show();
						dialog.setTitle(" Please enter your company name!");
						}else{
						dialog.show();
						dialog.setTitle("请输入您公司的名称");
					}
					
//					Toast.makeText(mContext, "请输入您公司的名称", 1).show();
					return;
				}

				if ("".equals(mParentActivity.getId())) {
					servicename = "insert";
				} else {
					servicename = "update";
				}
				requst();
			}

			private void requst() {
				List aa = new ArrayList();
				if (servicename.equals("insert")) {
					HashMap<String, String> data = new HashMap<String, String>();
					data.put("name", myname);
					data.put("company", name);
					data.put("qzkhly", "CloudCC");
					data.put("qzkhzt", "1，原始进入系统尚未处理");

					data.put("name", "安卓手机快速体验");
					data.put("phone", mParentActivity.getTestPhoneMobile());
					data.put("isMobileRegister", "true");
					aa.add(data);
				} else if (servicename.equals("update")) {
					HashMap<String, String> data = new HashMap<String, String>();
					data.put("name", myname);
					data.put("company", name);
					data.put("id", mParentActivity.getId());
					aa.add(data);
				} else {
					return;
				}
//				android.util.Log.d("ceshishuju","name"+name+"company"+myname);
				RequestParams params = new RequestParams();
				params.put("serviceName", servicename);
				params.put("objectApiName", "Lead");
				params.put("data", GsonUtil.Object2Json(aa));
				params.put("binding", mParentActivity.testBinding);
				AsyncClient.getInstance().post(
						"http://app.cloudcc.com/distributor.action", params,
						new myJsonRequest());

			}
		});

		edtmyname.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				// TODO Auto-generated method stub

			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
										  int after) {
				// TODO Auto-generated method stub

			}

			@Override
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub
				if (TextUtils.isEmpty(edtmyname.getText().toString()) || TextUtils.isEmpty(edtname.getText().toString())) {
					xinxitijiao.setEnabled(false);
					xinxitijiao.setBackgroundResource(R.drawable.shape_corner6_stroke1_b9b9b9_sloid);
				} else {
					xinxitijiao.setEnabled(true);
					xinxitijiao.setBackgroundResource(R.drawable.shape_corner6_stroke1_0d74d4_sloid);
				}

			}
		});

		edtname.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				// TODO Auto-generated method stub

			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
										  int after) {
				// TODO Auto-generated method stub

			}

			@Override
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub
				if (TextUtils.isEmpty(edtname.getText().toString()) || TextUtils.isEmpty(edtmyname.getText().toString())) {
					xinxitijiao.setEnabled(false);
					xinxitijiao.setBackgroundResource(R.drawable.shape_corner6_stroke1_b9b9b9_sloid);
				} else {
					xinxitijiao.setEnabled(true);
					xinxitijiao.setBackgroundResource(R.drawable.shape_corner6_stroke1_0d74d4_sloid);
				}

			}
		});
	}

	private class myJsonRequest extends JsonRequest {

		@Override
		public void handleFailure(ErrorInfo arg0) {
			dismissWainting();
			Log.d("dcd", "ssssss");
			MoveNextStep();
		}

		@Override
		public void handleSuccess(JsonObject arg0, String arg1) {
			dismissWainting();
			Log.d("dcd", "ssssss");
			MoveNextStep();
		}
	}

	// 判断email格式是否正确
//	public boolean isEmail(String email) {
//		String str = "^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$";
//		Pattern p = Pattern.compile(str);
//		Matcher m = p.matcher(email);
//		return m.matches();
//	}


	@Override
	public void onDestroy() {
		super.onDestroy();
		// cancelTimer();
	}

	@Override
	public int getLayoutId() {
		return R.layout.test_step2;
	}

}
